package com.btm.easymetadata.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.btm.easymetadata.server.entity.dao.ProductRequirementDocument;
import com.btm.easymetadata.server.entity.dao.ProductRequirementDocumentInfo;
import com.btm.easymetadata.server.entity.dao.uniondao.PrdAllInfo;
import com.btm.easymetadata.server.entity.dto.PrdDTO;
import com.btm.easymetadata.server.entity.dto.PrdInfoDTO;
import com.btm.easymetadata.server.entity.vo.PrdAllInfoVO;
import com.btm.easymetadata.server.entity.vo.PrdVO;
import com.btm.easymetadata.server.mapper.mybatisplus.ProductRequirementDocumentInfoMapper;
import com.btm.easymetadata.server.mapper.mybatisplus.ProductRequirementDocumentMapper;
import com.btm.easymetadata.server.util.DAOtoVOUtil;
import com.btm.easymetadata.server.util.DTOtoDAOUtil;
import com.btm.easymetadata.server.util.PrdCodeUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class PrdService {

    private final ProductRequirementDocumentMapper prdMapper;

    private final ProductRequirementDocumentInfoMapper prdInfoMapper;

    public boolean addPrd(PrdDTO prdDTO) {
        ProductRequirementDocument prd = DTOtoDAOUtil.INSTANCE.getPrdFromDTO(prdDTO);
        return 1 == prdMapper.insert(prd);
    }

    public boolean updatePrd(PrdDTO prdDTO) {
        if (Objects.isNull(prdDTO.getId())) {
            return false;
        }
        return 1 == prdMapper.updateById(DTOtoDAOUtil.INSTANCE.getPrdFromDTO(prdDTO));
    }

    public IPage<PrdVO> listPrd(PrdDTO prdDTO) {
        LambdaQueryWrapper<ProductRequirementDocument> wrapper = new LambdaQueryWrapper<>();
        IPage<ProductRequirementDocument> pages = new Page<>(prdDTO.getPageNumber(), prdDTO.getPageSize());
        IPage<ProductRequirementDocument> pageResult = prdMapper.selectPage(pages, wrapper);
        IPage<PrdVO> result = new Page<>();
        result.setCurrent(pageResult.getCurrent());
        result.setPages(pageResult.getPages());
        result.setSize(pageResult.getSize());
        result.setTotal(pageResult.getTotal());
        List<ProductRequirementDocument> records = pageResult.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            result.setRecords(records.stream().map(DAOtoVOUtil.INSTANCE::getPrdVOFromDTO).collect(Collectors.toList()));
        } else {
            result.setRecords(Collections.emptyList());
        }
        return result;
    }

    public boolean addPrdInfo(PrdInfoDTO prdInfoDTO) {
        String prdCode = prdInfoDTO.getPrdCode();
        String prdName = prdInfoDTO.getRequirementName();
        ProductRequirementDocument prd = null;
        if (StringUtils.hasText(prdCode) || StringUtils.hasText(prdName)) {
            LambdaQueryWrapper<ProductRequirementDocument> wrapper = new LambdaQueryWrapper<>();
            wrapper.or().eq(StringUtils.hasText(prdCode), ProductRequirementDocument::getPrdCode, prdCode)
                    .eq(StringUtils.hasText(prdName), ProductRequirementDocument::getPrdName, prdName);
            prd = prdMapper.selectOne(wrapper);
        } else {
            prdCode = PrdCodeUtil.buildCode();
        }
        if (Objects.isNull(prd)) {
            prd = new ProductRequirementDocument();
            prd.setPrdCode(prdCode);
            prd.setPrdName(prdInfoDTO.getRequirementName());
            prdMapper.insert(prd);
            prd = prdMapper.selectOne(new LambdaQueryWrapper<ProductRequirementDocument>().eq(ProductRequirementDocument::getPrdCode, prdCode));
        }
        ProductRequirementDocumentInfo prdInfo = DTOtoDAOUtil.INSTANCE.getPrdInfoFromDTO(prdInfoDTO);
        prdInfo.setPrdId(prd.getId());
        return 1 == prdInfoMapper.insert(prdInfo);
    }

    public boolean updatePrdInfo(PrdInfoDTO prdInfoDTO) {
        if (Objects.isNull(prdInfoDTO.getId())) {
            return false;
        }
        return 1 == prdInfoMapper.updateById(DTOtoDAOUtil.INSTANCE.getPrdInfoFromDTO(prdInfoDTO));
    }

    public IPage<PrdAllInfoVO> listPrdAllInfo(PrdInfoDTO prdInfoDTO) {
        List<PrdAllInfo> pageResult = prdInfoMapper.selectPrd(prdInfoDTO);
        IPage<PrdAllInfoVO> result = new Page<>();
        result.setCurrent(prdInfoDTO.getPageNumber());
        result.setPages(100);
        result.setSize(prdInfoDTO.getPageSize());
        result.setTotal(10000);
        if (!CollectionUtils.isEmpty(pageResult)) {
            result.setRecords(pageResult.stream().map(DAOtoVOUtil.INSTANCE::getPrdAllInfoVoFromDTO).collect(Collectors.toList()));
        } else {
            result.setRecords(Collections.emptyList());
        }
        return result;
    }
}
